package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.mappers.UserInfoMapper;
import com.tanhua.model.domain.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@DubboService
public class UserInfoApiImpl implements UserInfoApi{

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    //根据id查询用户信息
    @Override
    public UserInfo findById(Long userID) {
        return userInfoMapper.selectById(userID);
    }

    @Override
    public Map<Long, UserInfo> findByIds(List<Long> ids, UserInfo userInfo) {
        //构造条件构造器
        LambdaQueryWrapper<UserInfo> qw = new LambdaQueryWrapper<>();
        qw.in(UserInfo::getId,ids);

//        QueryWrapper<UserInfo> qw = new QueryWrapper();
//        //1、用户id列表
//        qw.in("id",ids);
        //2、添加筛选条件
        if(userInfo != null) {
            if(userInfo.getAge() != null) {
                qw.lt(UserInfo::getAge,userInfo.getAge());
            }
            if(!StringUtils.isEmpty(userInfo.getGender())) {
                qw.eq(UserInfo::getGender,userInfo.getGender());
            }
        }
        List<UserInfo> list = userInfoMapper.selectList(qw);
        Map<Long, UserInfo> map = CollUtil.fieldValueMap(list, "id");
        return map;
    }
}
